package com.thiscc.sum.logic;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.mongodb.util.JSON;
import com.thiscc.sum.dao.impl.CommonDaoImpl;
import com.thiscc.sum.utils.DBObjectUtils;
import com.thiscc.sum.utils.DateUtils;
import com.thiscc.sum.utils.JSONUtils;
import com.thiscc.sum.utils.RequestUtils;
import com.thiscc.sum.utils.StringUtils;
import com.thiscc.sum.utils.TableNameConstant;


/**
 * 总结关注
 * @author helios
 * 
 */
public class CareLogic{

	private CommonDaoImpl commonDao = new CommonDaoImpl();
	
	/*-------------------------------------------------------------------------------------------*/
	
	/**
	 * 根据条件查询我关注的总结
	 * @param request	uuid:要查询的人uuid
	 * @param response	当前人所有关注的总结
	 * @author helios
	 * 2014-12-26 helios修改 添加分页查询,按关注时间倒叙，在第一条数据中返回关注总数
	 */
	public List<DBObject> myCareList(HttpServletRequest request, HttpServletResponse response){
		
		//获取当前人的uuid,拼接查询条件
		String s_uuid = RequestUtils.getString(request, "uuid");
		
		if(StringUtils.isBlank(s_uuid) || "null".equals(s_uuid) || "undefined".equals(s_uuid)){
			s_uuid = (request.getSession().getAttribute("uuid")).toString();
		}
		
		int i_pageNo = RequestUtils.getInt(request, "pageNo");
		int i_perPageCount = RequestUtils.getInt(request, "pageSize");
		
		DBObject DBO_condition = new BasicDBObject("uuid",s_uuid);
		
		//根据uuid查询关注表，获取总结编号、关注内容、是否已读、创建时间,按关注日期排序，倒叙
		JSONObject JSO_ret = (JSONObject) commonDao.searchCommonPage(TableNameConstant.T_SUMMARIZE_ATTENTION, DBO_condition, 
				new BasicDBObject("atime",-1), i_pageNo, i_perPageCount);
		JSONArray JSY_ret = JSO_ret.getJSONArray("data");
		
		List<DBObject> l_ret = new ArrayList<DBObject>();//要返回的数据
		
		DBObject DBO_ret=null;              
		String s_oid ="";                     //总结编码
		Date DT_ctime = null;                  //创建日期
		
		DBObject DBO_summary = null;  
		
		JSONArray JSY_content =null;  //关注表中的数据JSONArray
		JSONArray JSY_contents = null;   //总结表中的详细数据
		
		String s_title="";               //用于接收每一条关注中的标题
		String s_lev="";                 //用于接收每一条关注当中的第几条
		
		String s_title2="";              //用于接收总结表中的标题
		String s_lev2="";                //用于接收总结表中的第几条
		
		
		JSONObject JSO_content = null;
		
		Map<String,List<String>> contentMap = null;
		
		for(int k=0,len=JSY_ret.size();k<len;k++ ){
			
			JSONObject JSO_bean = JSY_ret.getJSONObject(k);
			DBObject careBean = (DBObject)JSON.parse(JSO_bean.toString());
			
			s_oid = DBObjectUtils.getString(careBean, "oid");
			DT_ctime = DBObjectUtils.getDate(careBean, "atime");//创建日期

			//根据总结编号查询总结表，获取总结类型、标题		
			DBO_condition = new BasicDBObject();
			DBO_condition.put("oid", s_oid);

			DBO_summary = commonDao.searchCommonOne(TableNameConstant.T_SUMMARIZE_CONTENT, DBO_condition);

			JSY_contents = DBObjectUtils.getJSONArray(DBO_summary, "content");             //总结表中的详细数据
			
			contentMap = new HashMap<String,List<String>>();        //存放返回值
			
			List<DBObject> contentList=null;
			
			JSY_content = DBObjectUtils.getJSONArray(careBean, "content");          //关注表中的数据
		
			for(int i=0;i<JSY_content.size();i++){              //循环关注表数据
				
				JSONObject contentBean = JSY_content.getJSONObject(i);
				s_title = JSONUtils.getString(contentBean,"title");
				s_lev = JSONUtils.getString(contentBean,"lev");
				
				for(int j=0;j<JSY_contents.size();j++){            //循环总结表中的数据
					
					JSO_content = JSY_contents.getJSONObject(j);
					s_title2 = JSONUtils.getString(JSO_content, "title");
					s_lev2 = JSONUtils.getString(JSO_content,"lev");
					
					if(s_title.equals(s_title2) && s_lev.equals(s_lev2)){
						
						String value = JSONUtils.getString(JSO_content, "value");
						
						List<String> list = contentMap.get(s_title);
						
						if(list!=null){
							list.add(value);
						}else{
							list = new ArrayList<String>();
							list.add(value);
							contentMap.put(s_title, list);
						}
						break;
					}
				}
			}
			
			contentList = new ArrayList<DBObject>(contentMap.size());
			
			Set<String> keySet = contentMap.keySet();
			DBObject bean;
			for(String key:keySet){
				bean = new BasicDBObject();
				bean.put("title", key);
				bean.put("value", contentMap.get(key));
				contentList.add(bean);
			}
			
			DBO_ret = new BasicDBObject();
			DBO_ret.put("oid", s_oid);//编号
			DBO_ret.put("state", DBObjectUtils.getString(careBean,"state"));//是否已读
			DBO_ret.put("ctime", DateUtils.dateToString(DT_ctime, "MM-dd HH:mm"));
			DBO_ret.put("classify", DBObjectUtils.getString(DBO_summary, "classify"));//类型
			DBO_ret.put("title", DBObjectUtils.getString(DBO_summary, "title"));//标题
			DBO_ret.put("content", contentList);
			l_ret.add(DBO_ret);
		}
		
		//在第一条数据中添加关注的总条数
		if(l_ret.size()>0){
			l_ret.get(0).put("total", JSO_ret.getInt("total"));
		}
		
		return l_ret;
	}
	
	
	/*-------------------------------------------------------------------------------------------*/
	/**
	 * @param uuid //登陆人的uuid
	 * @param oid //总结的唯一编号
	 * @param title//关注的标题
	 * @param lev//关注第几条
	 * @param ctime 发总结的时间
	 *	关注总结   确认关注
	 * @author 易泉 2014年12月17日
	 */
	public String attentionPerson(HttpServletRequest request){
		
		//拿到一条JSON数据
		String s_attention = RequestUtils.getString(request, "attention");
		JSONObject JSY_attentionArr = JSONUtils.getJsonObject(s_attention);
		
		String uuid = JSONUtils.getString(JSY_attentionArr, "uuid");
		
		if(StringUtils.isBlank(uuid) || "null".equals(uuid)){
			uuid = (request.getSession().getAttribute("uuid")).toString();
		}
		
		//添加数据条件
		DBObject DBO_insertBean = new BasicDBObject();
		DBO_insertBean.put("uuid", uuid);
		DBO_insertBean.put("oid",JSONUtils.getString(JSY_attentionArr,"oid"));
		
		JSONArray JSY_contentArr = JSONUtils.getJSONArray(JSY_attentionArr, "content");
		JSONArray JSY_cttArr = new JSONArray();
		JSONObject JSY_contentObj = null;
		
		for (int i = 0; i < JSY_contentArr.size(); i++) {
			
			JSY_contentObj = JSY_contentArr.getJSONObject(i);
			
			JSY_contentObj.put("title", JSONUtils.getString(JSY_contentObj, "title"));
			JSY_contentObj.put("lev", JSONUtils.getString(JSY_contentObj, "lev"));
			
			JSY_cttArr.add(JSY_contentObj);
		}
		
		DBO_insertBean.put("content", JSY_cttArr);
		DBO_insertBean.put("state","2");
		DBO_insertBean.put("atime",new Date());
		
		String ctime = JSONUtils.getString(JSY_attentionArr,"ctime");
		DBO_insertBean.put("ctime",DateUtils.stringToDate(ctime, DateUtils.DATE_FORMAT_SS));
		
		boolean b_flag = commonDao.insertCommon(TableNameConstant.T_SUMMARIZE_ATTENTION, DBO_insertBean);
		
		//关注是否修改成功  1为成功  2为失败
		return b_flag?"1":"2";
	}
}
